﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using System.IO;
using System.Globalization;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.UserManagement.DeptMgr
{
    public partial class DeptMgrUserControl : UserControl
    {
        static string JCYUrl = System.Configuration.ConfigurationManager.AppSettings["JCYUrl"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            string removeDept = Request.Params.Get("__EVENTTARGET");

            if (!IsPostBack)
            {
                BindDeptGridView();


            }

            if (removeDept != null && removeDept.IndexOf("DeleteDeptById") > -1)
            {
                ButtonRemoveDept_Click(null, null);
            }
        }

        public void ButtonRemoveDept_Click(object sender, EventArgs e)
        {
            int deptId = Int32.Parse(this.hfSelId.Value);
            RemoveSelectedRows(deptId);
        }

        private void BindDeptGridView()
        {
            UserManagementProxy proxy = new UserManagementProxy();
            DepartmentTitleDataSet.T_DeptTitleDataTable dt = proxy.GetDeptTitleList();
            GridViewDeptList.DataSource = dt;
            GridViewDeptList.DataBind();
        }

        protected void GridViewDeptList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ////Set Select checkbox's attribute
                //Label lblId = (Label)e.Row.FindControl("lblId");
                //RadioButton cbSelect = (RadioButton)e.Row.FindControl("cbSelect");
                //cbSelect.Attributes.Add("onclick", "ClickSelectCheckBox('" + cbSelect.ClientID + "','" + lblId.Text + "');");
            }
        }

        protected void GridViewDeptList_DataBound(object sender, EventArgs e)
        {

        }

        protected void lbEdit_Click(object sender, EventArgs e)
        {
            string selDeptId = this.hfSelId.Value;
            if (selDeptId.Trim() != "")
                Response.Redirect("DeptEdit.aspx?selDeptId=" + selDeptId);
        }



        protected void RemoveSelectedRows(int deptId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                proxy.DelDeptById(deptId);

                DataSetUserManagement.T_DepartmentDataTable dept = proxy.GetDeptById(deptId, string.Empty);
                if (dept.Rows.Count > 0)
                {
                    DeleteDocLibrary(dept[0].DeptName);
                    DeleteDocLibrary(dept[0].DeptName + " 讨论版");
                }
            }

            BindDeptGridView();

        }

        private void DeleteDocLibrary(string docName)
        {
             SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(JCYUrl))
                {
                    using (SPWeb myWeb = site.AllWebs["MyDept"])
                    {
                        myWeb.AllowUnsafeUpdates = true;

                        // delete old doc and old group
                        SPList oldDept = myWeb.Lists[docName];
                        if (!oldDept.HasUniqueRoleAssignments)
                        {
                            oldDept.BreakRoleInheritance(true);
                            SPRoleAssignmentCollection assignmentColOld = oldDept.RoleAssignments;
                            for (int i = assignmentColOld.Count - 1; i >= 0; i--)
                            {
                                assignmentColOld.Remove(i);
                            }
                            myWeb.Groups.Remove(docName);
                        }
                        myWeb.Lists.Delete(oldDept.ID);
                        myWeb.AllowUnsafeUpdates = false;

                        myWeb.Dispose();
                        site.Dispose();
                  }
                }
            });
        }

        public bool IsExistGroup(string groupname)
        {
            bool exist = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(JCYUrl))
                {
                    using (SPWeb web = site.AllWebs[JCYConstant.SiteName.MyDept])
                    {

                        SPGroupCollection groups = web.SiteGroups;
                        foreach (SPGroup grouplist in groups) //判断组是否存在
                        {
                            if (grouplist.ToString().ToLower() == groupname.ToLower())
                                exist = true;
                        }
                    }
                }
            });

            return exist;


        }
    }
}
